<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>コントロールパネル</title>
    
    <link href="./assets/styles/default.css" rel="stylesheet" />
    <script src="./assets/scripts/libs/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script src="./assets/scripts/import.js" type="text/javascript"></script>
    <script type="text/javascript">
	
		var st = function(c){
			$('#banners input').click(function(){ $(this).focus(); });
			$("#banners").sortable({
				placeholder : "banner_placeholder",
				start: function(event, ui) {
					$("body").append("<div id='righttopmsg'>並べたい位置に移動して、クリックを離して下さい。</div>");
					$('#righttopmsg').show(300);
				},
				stop: function(event, ui) {
					$('#righttopmsg').fadeOut(300,function(){ $(this).remove(); });
					
					$('#banners table').each(function(i){
						$('input[type=file]',this).attr("name","image[" + i + "]");
						$('input[type=text]:eq(0)',this).attr("name","forms[alt_text][" + i + "]");
						$('input[type=text]:eq(1)',this).attr("name","forms[url][" + i + "]");
						$('input[type=hidden]:eq(0)',this).attr("name","forms[original][" + i + "]");
						$('input[type=hidden]:eq(1)',this).attr("name","forms[temp_name][" + i + "]");
						$('input[type=radio]',this).attr("name","forms[target][" + i + "]");
						$('label.checked input',this).attr("checked",true);
					});
				}
			});
			$("#banners table").disableSelection();
		}

	
		var del_row = function(){
			$('.delete_category').unbind();
			$('.delete_category').click(function(){	
				var f = function(){
					thisthis.closest("table").remove();
					$('#banners table').each(function(i){
						$('input[type=file]',this).attr("name","image[" + i + "]");
						$('input[type=text]:eq(0)',this).attr("name","forms[alt_text][" + i + "]");
						$('input[type=text]:eq(1)',this).attr("name","forms[url][" + i + "]");
						$('input[type=radio]:eq(0)',this).attr("name","forms[target][" + i + "]");
						$('input[type=radio]:eq(1)',this).attr("name","forms[target][" + i + "]");
					});
				}
			
				var thisthis = $(this);	
				var v = $('input[type=text]:eq(1)',$(this).closest('tr')).val();
				var v2 = $('input[type=hidden]:eq(0)',$(this).closest('tr')).val();
				if(typeof v != "undefined" || typeof v2 != "undefined"){					
					customConfirm('削除しますか？',"",$(this),function(){
						f();
					});
				}else {
					f();
				}
			});
		}
		$(function() {
			st();
			back_to_search();
			$('.btn_regist,#btn_regist').click(function(e){
				e.preventDefault();
				if(form_validation() == true){
					customConfirm("{if $forms.id ==''}登録{else}更新{/if}してよろしいですか？","",$(this),function(){
						if( document.fm.img1.value != '' ){
							if( confirm('未アップロードの画像がありますがよろしいですか？') ){
								document.fm.submit();
							}
						}else{
							document.fm.submit();
						}
					});
				}else {
					validateError();
				}
			});
			
			//$('#banner_update').attr("href",document.referrer);
			
			
			//項目追加
			$('.btns .left a').click(function(){
				var h = '<table class="formTable"><tbody><tr><th><a href="javascript:;" class="delete_category">削除</a>画像</th><td><input type="file" name="image[99]" class="uploadPic" /></td></tr><tr><th>名称</th><td><input type="text" name="forms[alt_text][99]" value="" /></td></tr><tr><th>URL</th><td><input type="text" name="forms[url][99]" class="text" value=""><br><label class="radio checked"><input type="radio" name="forms[target][99]" value="0" checked="checked">同ウインドウ</label><label class="radio"><input type="radio" name="forms[target][99]" value="1">別ウインドウ</label></td></tr></tbody></table>';
				$('#banners').append(h);
				$('#banners table').each(function(i){
					$('input[type=file]',this).attr("name","image[" + i + "]");
					$('input[type=text]:eq(0)',this).attr("name","forms[alt_text][" + i + "]");
					$('input[type=text]:eq(1)',this).attr("name","forms[url][" + i + "]");
					$('input[type=radio]:eq(0)',this).attr("name","forms[target][" + i + "]");
					$('input[type=radio]:eq(1)',this).attr("name","forms[target][" + i + "]");
				});
				var form = $('form[name=fm]',form);
				$('.uploadPic').unbind().change(function(e){
					loading();
					pushID();
					$('input[name=mode]',form).val('upload');
					form.submit();
				});
				del_row();
				st();
			});
			
			//空の項目を全て削除
			$('.btns .right a').click(function(){
				$('#banners table').each(function(){
					if($("input[type=text]:eq(1)",this).val() == "" && $("input[type=hidden]:eq(0)",this).size() == 0){
						$(".delete_category",this).click();
					}
				});
			});
			del_row();
				
		});
	</script>
	<style type="text/css">
		#banners table {
			margin-bottom:10px;
			border:1px solid #ddd;
			position:relative;
		}
		.banner_placeholder {
			filter: alpha(opacity=20);
			-moz-opacity:0.20;
			opacity:0.20;
			background:#0099ff !important;
			box-shadow:0 0 0;
			width:100%;
			height:100px !important;
		}
	</style>
    
</head>
<body>

{include file='include/header.inc.html'}

<div id="container">
	<aside id="accordion">
    	<div id="aside_inner">
    		{include file='include/menu.inc.html'}
        </div>
    </aside>
    
    <article>
    	<h2><span>&nbsp;</span>Banner<p><a href="javascript:;">Open Plugins</a></p></h2>
        <nav>
        	<ul>
            	<li class="active"><a href="{$smarty.server.SCRIPT_NAME}"><span>スケジューラ</span></a></li>
            	<li><a href="banner_config.php"><span>種別マスタ</span></a></li>
			</ul>
        </nav>
        
        <section id="content">
                {if isset($id)}{assign var=operation value='更新'}{else}{assign var=operation value='登録'}{/if} 
        	<h3>スケジューラ</h3>
                
                {if $phase == 'input'}
                
                <p class="message">内容を確認し、{if $forms.id == ''}登録{else}更新{/if}ボタンをクリックしてください。</p>
				
                <form name="fm" action="{$smarty.server.SCRIPT_NAME}" method="post" enctype="multipart/form-data">
                        <input type="hidden" name="mode" value="input" />
                        
                        <ul class="t_btns">
                            {if $forms.id != ''}
                                <li class="left delete"><button type="button" class="chk_delete"><em>&nbsp;</em>削除</button></li>
                                <li class="right refresh"><button type="button" class="chk_update"><em>&nbsp;</em>更新</button></li>
                            {else}
                                <li class="single add"><button type="button" class="chk_add"><em>&nbsp;</em>登録</button></li>
                            {/if}
                        </ul>

                        
                        <h4><span>&nbsp;</span>基本情報</h4>
                        
                        {include file='banner/include/form.inc.html'}
						
                            <p class="button">
				{if $id == ''}
                                    <button type="button" id="btn_regist" class="submit"><span>登録</span></button>
				{else}
                                    <button type="button" id="btn_delete" class="submit"><span>削除</span></button>
                                    <button type="button" id="btn_regist" class="submit"><span>更新</span></button>
				{/if}
                            </p>
                                    <input type="hidden" name="id" value="{$id}" />
                                    <input type="hidden" name="do" value="{if $operation == '更新'}validate_edit_form{else}validate_regist_form{/if}"/>
                                </form>
				{/if}
				
				
				{if $phase == 'complete'}
					<p class="message">{$message}</p>
				
					<ul class="result_btn">
                                            <li><a href="{$smarty.server.SCRIPT_NAME}?y={$year}&m={$month}">スケジューラに戻る</a></li>
                                            {*if $forms.year != ''*}
                                            {if isset($id)}
                                                <li><a id="banner_update" href="{$smarty.server.SCRIPT_NAME}?op=edit&y={$year}&m={$month}&d={$day}&b={$banner_type}">登録した内容を更新する</a></li>
                                            {/if}
					</ul>
				{/if}
	        </section>
    </article>
    
</div>

{include file='include/footer.inc.html'}

</body>
</html>